package cn.gson.crm.controller.potential;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.StudentDao;
import cn.gson.crm.model.entity.Student;

/**
 * 导入学生
 */
@WebServlet("/student/add")
public class StudentAdd extends BaseServlet {
	private static final long serialVersionUID = 1L;
    private StudentDao stuDao = new StudentDao();
	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.get(request, response);
		String sid = request.getParameter("sid");
		if(sid != null && !sid.isEmpty()){
			Student stu = stuDao.getStudentById("正常", "未分配", Integer.parseInt(sid));
			request.setAttribute("stu", stu);
		}
		forward("student/student_add.jsp");
	}
	@Override
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.post(request, response);
		String sid = request.getParameter("sid");
		String name = request.getParameter("userName");
		String source = request.getParameter("source");
		String gender = request.getParameter("gender");
		String education = request.getParameter("education");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String idCard = request.getParameter("idCard");
		String qq = request.getParameter("qq");
		String fatherName = request.getParameter("fatherName");
		String fatherTel = request.getParameter("fatherTel");
		String motherName = request.getParameter("motherName");
		String motherTel = request.getParameter("motherTel");
		
		if(name == null || name.isEmpty()){
			request.setAttribute("erro", "名字不能为空!");
			get(request, response);
			return;
		}
		if(gender == null || gender.isEmpty()){
			request.setAttribute("erro", "性别不能为空!");
			get(request, response);
			return;
		}
		if(source == null || source.isEmpty()){
			request.setAttribute("erro", "来源一定要写哦!");
			get(request, response);
			return;
		}
		if(phone == null || phone.isEmpty()){
			request.setAttribute("erro", "电话是联系必须品！");
			get(request, response);
			return;
		}
		
		Student s = new Student();
		if(sid != null && !sid.isEmpty()){
			s.setId(Integer.parseInt(sid));
		}
		
		s.setName(name);
		s.setGender(gender);
		s.setSource(source);
		s.setEducation(education);
		s.setPhone(phone);
		s.setAddress(address);
		s.setIdCard(idCard);
		s.setQq(qq);
		s.setFatherName(fatherName);
		s.setFatherTel(fatherTel);
		s.setMotherName(motherName);
		s.setMotherTel(motherTel);
		s.setIsAllocation("未分配");
		s.setStatus("正常");
		
		if(stuDao.saveStu(s)){
			redirect("potential");
			return;
		}
		request.setAttribute("erro", "保存失败!");
		get(request, response);
	}
}
